#include <vector>
using namespace std;
/*
 * @lc app=leetcode.cn id=59 lang=cpp
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> matrix(n, vector<int>(n, -1));
        int i = 1, round = 0, row = 0, col = -1;
        while (i <= n * n) {
            while (++col < n && matrix[row][col] == -1) {
                matrix[row][col] = i++;
            }
            col--;
            while (++row < n && matrix[row][col] == -1) {
                matrix[row][col] = i++;
            }
            row--;
            while (--col >= 0 && matrix[row][col] == -1) {
                matrix[row][col] = i++;
            }
            col++;
            while (--row >= 0 && matrix[row][col] == -1) {
                matrix[row][col] = i++;
            }
            row++;
        }
        return matrix;
    }
};
// @lc code=end

